<template>
	<view class="container">
		<view class="close-icon">
			<!-- <u-icon name="close" color="A2A2A2" v-show="filePath" ></u-icon> -->
		</view>
		<view class="check-box">
			<view class="choose-img" @click="chooseImg" >
				<u-icon 
					name="camera" 
					color="666666" 
					size="60" 					
					v-show="!filePath">
				</u-icon>
				<image 
					class="fileSize"
					:src="filePath" 
					v-show="filePath" >
				</image>
			</view>
			<view class="select-item">
				<SelectBox @select="onSelect($event, 'checkList')" size="small" :list="checkList" lableName="typeName"/>
			</view>
		</view>
	</view>
</template>

<script>
	import SelectBox from '@/components/lt-checkbox/index.vue';
	import { getVehicleCheckIds } from '@/api/carPreFlight.js';
	import config from '@/config/index.js'
	import storage from '@/utils/Stroage.js'
	export default {
		components:{
			SelectBox
		},
		props:{
			itemIndex:{type:Number,default:0}
		},		
		mounted(){
			if(this.formType ==='add'){
				getVehicleCheckIds().then(res=>{
					console.log('getVehicleCheckIds',res)
					 this.checkList = res.rows
				})
			}
			
		},
		computed:{
			formType(){
				return this.$store.state.carPreFlight.formType
			},
			returnImgSrc(){
				let imgList = this.$store.state.carPreFlight.formDetail.pictureVOS || []
				if(this.formType !== 'add'){
					this.filePath = config.config.sysUrl + imgList[this.itemIndex].picPath
					this.checkList = imgList[this.itemIndex].list
				}
			}
		},
		data(){
			return{				
				checkList:[],
				filePath:null,
				picPath:null
			}
		},
		methods:{
			onSelect(v, field){
				if(this.$store.getters.isEdit) return
				if(!this.picPath){
					uni.showToast({
						title:'请先选择或拍摄损坏部位照片',
						icon:'none'
					})
					return
				}
				const list = this[field];
				list.map(item=>{
					item.valueCode = '0'
					return item 
				})
				list[v].valueCode = list[v].valueCode === '0' ? '1' : '0';
				let checkedVals = {};
				list.forEach(item => {
					if(item.valueCode === '1') {
						checkedVals = item 
					}
				})
				const obj = {
					picPath:this.picPath
				}
				for(let key in checkedVals){
					obj[key] = checkedVals[key]
				}
				this.$emit('getPointInfo',obj)
				console.log('checkedVals',checkedVals)
			},
			chooseImg(){
				if(this.$store.getters.isEdit) return
				const upLoadUrl = config.config.sysUrl + '/mp/file/notice/upload.do'
				console.log('upLoadUrl',upLoadUrl)
				const that = this 
				uni.chooseImage({
				  success (res) {
				    const tempFilePaths = res.tempFilePaths
					that.filePath = tempFilePaths[0]
					console.log(that.filePath)
				    uni.uploadFile({
				      url: upLoadUrl, 
				      filePath: tempFilePaths[0],
				      name: 'uploadfile',
					  header:{
					  	'Authorization':storage.get('token')
					  },
				      success (res){
				        let data = JSON.parse(res.data)
						that.picPath = data.path[0][0].substring(data.path[0][0].indexOf('|') + 1) 						
				      }
				    })
				  }
				})
			}
		}
	}
</script>

<style lang="scss" scoped>
	@import './index.scss';
</style>
